package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.GalaxyCardPromoteUserChannel.GALAXY_CARD_PROMOTE_USER_CHANNEL;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jooq.Record3;
import org.jooq.Select;
import org.jooq.SelectConditionStep;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.custom.GalaxyCardPromoteUserRelateChannelCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPromoteUserRelateChannelRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPromoteUserRelateChannelResponse;

@SuppressWarnings({ "rawtypes" })
@Repository
public class GalaxyCardPromoteUserRelateChannelRepositoryImpl extends JOOQBaseRepositoryPlus implements GalaxyCardPromoteUserRelateChannelCustomRepository{
	
	@Override
	protected TableImpl getTable() {
		return COMM_PRODUCT_CHANNEL;
	}

	@Override
	public Page<GalaxyCardPromoteUserRelateChannelResponse> list(
			GalaxyCardPromoteUserRelateChannelRequest searchRequest,
			Pageable pageable) {
		
		Select subQueryRelatedChannel = dslContext.select(GALAXY_CARD_PROMOTE_USER_CHANNEL.CHANNEL_CODE)
				  .from(GALAXY_CARD_PROMOTE_USER_CHANNEL)
				  .where(GALAXY_CARD_PROMOTE_USER_CHANNEL.USER_ID.eq(searchRequest.getUserId()));
		
		SelectConditionStep<Record3<Integer, String, Integer>> selectOnConditionStepFinal = dslContext.select(
				COMM_PRODUCT_CHANNEL.CHANNEL_CODE.as("channelCode1"), 
				COMM_PRODUCT_CHANNEL.CHANNEL_NAME.as("channelName"), 
				GALAXY_CARD_PROMOTE_USER_CHANNEL.CHANNEL_CODE.as("channelCode2"))
		.from(COMM_PRODUCT_CHANNEL).leftJoin(subQueryRelatedChannel.asTable("galaxy_card_promote_user_channel"))
		.on(COMM_PRODUCT_CHANNEL.CHANNEL_CODE.eq(GALAXY_CARD_PROMOTE_USER_CHANNEL.CHANNEL_CODE)).where(" 1 = 1");
		
		if(searchRequest.getChannelCode() != null){
			selectOnConditionStepFinal.and(COMM_PRODUCT_CHANNEL.CHANNEL_CODE.eq(searchRequest.getChannelCode()));
		}
		
		if(StringUtils.isNotEmpty(searchRequest.getChannelName())){
			selectOnConditionStepFinal.and(COMM_PRODUCT_CHANNEL.CHANNEL_NAME.eq(searchRequest.getChannelName()));
		}
		
		return search(selectOnConditionStepFinal.getQuery(), pageable, record -> {
			 GalaxyCardPromoteUserRelateChannelResponse response = new GalaxyCardPromoteUserRelateChannelResponse();
			    response.setChannelCode((Integer) record.get("channelCode1"));
			    response.setChannelName((String) record.get("channelName"));
			    response.setRelatedChannel(record.get("channelCode2") != null);
	            return response;
	        });
		
	}


	@Override
	public List<Integer> findChannelCodeListByUserId(String userId) {
		return dslContext.select(GALAXY_CARD_PROMOTE_USER_CHANNEL.CHANNEL_CODE)
				.from(GALAXY_CARD_PROMOTE_USER_CHANNEL)
				.where(GALAXY_CARD_PROMOTE_USER_CHANNEL.USER_ID.eq(userId))
				.fetch(GALAXY_CARD_PROMOTE_USER_CHANNEL.CHANNEL_CODE);
	}

}
